package org.ielse.frame.test;

import org.ielse.frame.BaseActivity;
import org.ielse.frame.R;
import org.ielse.frame.test.fragment.MainFragment;
import org.ielse.frame.test.fragment.RightMenuFragment;
import org.ielse.frame.util.LogUtil;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;

import com.nineoldandroids.view.ViewHelper;

public class MainActivity extends BaseActivity {

	private MainFragment mainFragment;
	private RightMenuFragment rightMenuFragment;

	private DrawerLayout mDrawerLayout;

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main);
		super.onCreate(savedInstanceState);

		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction trans = fragmentManager.beginTransaction();
		mainFragment = new MainFragment();
		trans.replace(R.id.lay_content, mainFragment);
		rightMenuFragment = new RightMenuFragment();
		trans.replace(R.id.lay_right_menu, rightMenuFragment);
		trans.commit();

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerLayout.setScrimColor(Color.TRANSPARENT);
		mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
		mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
			@Override public void onDrawerStateChanged(int newState) {
				LogUtil.d("mDrawerLayout onDrawerStateChanged:" + newState);
			}

			@Override public void onDrawerSlide(View drawerView, float slideOffset) {
				View mContent = mDrawerLayout.getChildAt(0);
				final float contentScale = 1 - slideOffset * 0.1f;

				if (drawerView.getTag().equals("RIGHT")) {
					ViewHelper.setTranslationX(mContent, -drawerView.getMeasuredWidth() * slideOffset);
					ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
					ViewHelper.setPivotY(mContent, mContent.getMeasuredHeight() / 2);
					ViewHelper.setScaleY(mContent, contentScale);
					mContent.invalidate();
				}

				mainFragment.setActionViewAnimationProgress(slideOffset);
			}

			@Override public void onDrawerOpened(android.view.View drawerView) {
				rightMenuFragment.startContentAnimation();
			}

			@Override public void onDrawerClosed(android.view.View drawerView) {
				mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
				rightMenuFragment.resetContentAnimation();
			}
		});
	}

	public void openRightMenu() {
		mDrawerLayout.openDrawer(Gravity.RIGHT);
		mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);
	}
}
